<?php
/**
 * Created by cmb_weixin.master.
 * File: Distributor.php
 * User: xuewl
 * Date: 2017/12/17
 * Time: 17:15
 */

namespace app\api\controller\v1\user;
use app\api\controller\v1\Init;

class Distributor extends Init
{


    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 分销商申请
     * @return \think\Response
     */
    public function apply() {

        $info = $this->params;
        $info['license_pic'] = is_array($info['license_pic']) && !empty($info['license_pic']) ? implode(",", $info['license_pic']) : '';
        $info['idcard_pic'] = is_array($info['idcard_pic']) && !empty($info['idcard_pic']) ? implode(",", $info['idcard_pic']) : '';
        $info['works_pic'] = is_array($info['works_pic']) && !empty($info['works_pic']) ? implode(",", $info['works_pic']) : '';

        $result = model('user/distributor', 'service')->apply($this->user['id'], $info);
        if(!$result) {
            return $this->response(model('user/distributor', 'service')->getError(), [], 2000);
        }
        return $this->response('操作成功', $result);
    }

    /**
     * 分销商审批
     */
    public function review() {
        $result = model('user/distributor', 'service')->review($this->params['id'], $this->params['status'], $this->params['message']);
        if(!$result) {
            return $this->response(model('user/distributor', 'service')->getError(), [], 2000);
        }
        return $this->response('操作成功', $result);
    }


    /**
     * 分销商申请状态
     * @return \think\Response
     */
    public function status() {
        $maps = [
            'uid' => $this->user['id'],
            //'status' => ['neq', 1]
        ];
        $result = \think\Db::name('user_distributor')
            ->where($maps)
            ->field('id,status')
            ->order("review_time desc")
            ->value('status');
        return $this->response('success', $result);
    }

    /**
     * 业绩列表
     * @return \think\Response
     */
    public function product_order_lists() {
        $maps = [];
        $maps['distributor_id|customer_id'] = $this->user['id'];
        if(isset($this->params['stage'])) {
            $maps['stage'] = ["in", array_values($this->params['stage'])];
        }

        if(isset($this->params['status'])) {
            $maps['status'] = (int) $this->params['status'];
        }

        $order = $this->params['order'];
        $extra = $this->params['extra'] && !is_array($this->params['extra']) ? explode(",", $this->params['extra']) : [];
        $adds = [];
        $result = model('product/product_order', 'service')->lists($maps, $this->limit, $this->page, $order, true, $extra, $adds);
        return $this->response(lang('Operation completed'), $result);
    }

}